package com.qfedu.service;

import com.qfedu.dao.ConsumerDao;
import com.qfedu.entity.Consumer;
import com.qfedu.entity.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
public class ConsumerServiceImpl implements ConsumerService{

    @Autowired
    private ConsumerDao consumerDao;

    /**
     * 登录
     * @param consumer 封装的用户名及密码
     * @return
     */
    @Override
    public ResponseData<List<Consumer>> loginIn(Consumer consumer) {

        ResponseData<List<Consumer>> responseData = new ResponseData<>();

        Consumer user = consumerDao.loginIn(consumer);

        if (user != null){

            if (user.getPassword().equals(consumer.getPassword())){

                responseData.setCode(1);

                responseData.setMsg("登录成功");

                responseData.setUserMsg(Arrays.asList(user));

            }

        } else {
            responseData.setCode(0);
        }
        return responseData;
    }

    /**
     * 注册
     * @param consumer
     * @return
     */
    @Override
    public ResponseData<String> SignUp(Consumer consumer) {

        ResponseData<String> responseData = new ResponseData<>();

        consumer.setCreateTime(new Date());
        consumer.setUpdateTime(new Date());
        consumer.setAvator("/img/avatorImages/默认头像.png");

        int result = consumerDao.SignUp(consumer);

        if (result == 1){

            responseData.setMsg("注册成功！");

            responseData.setCode(1);

        } else {

            responseData.setCode(0);

        }
        return responseData;
    }

    /**
     * 更新用户信息
     * @param consumer
     * @return
     */
    @Override
    public ResponseData<String> updateUserMsg(Consumer consumer) {

        ResponseData<String> responseData = new ResponseData<>();

        int result = consumerDao.updateUserMsg(consumer);

        if (result == 1){

            responseData.setCode(1);

        } else {
            responseData.setCode(0);
        }

        return responseData;
    }

    /**
     * 返回指定ID的用户
     * @param id
     * @return
     */
    @Override
    public List<Consumer> getUserOfId(int id) {

        List<Consumer> consumerList = consumerDao.getUserOfId(id);

        if (consumerList != null && consumerList.get(0) != null){

            return consumerList;
        }

        return null;
    }

    @Override
    public ResponseData<String> updateAvator(int id, String avator) {

        ResponseData<String> responseData = new ResponseData<>();

        int result = consumerDao.updateAvator(id,avator);

        if (result == 1){

            responseData.setCode(1);

            responseData.setAvator(avator);
        } else {

            responseData.setCode(0);
        }
        return responseData;
    }

    @Override
    public List<Consumer> selectcha() {
        return consumerDao.selectcha();
    }

    @Override
    public boolean deleteshan(int id){
        return consumerDao.deleteshan(id);
    }




}
